package cn.itsource.pethome.user.domain;

import cn.hutool.core.lang.RegexPool;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.digest.MD5;
import cn.itsource.pethome.base.domain.BaseDomain;
import cn.itsource.pethome.base.domain.CreatePassword;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.util.Date;

/**
 * @author: hong
 * @Description:
 * @Date: Created in 12:34 2022/2/25
 * @Modified: By:
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "t_user")
public class User implements BaseDomain ,CreatePassword {
    public interface PhoneRegister{}
    public interface EmailRegister{}
@Id
@GeneratedValue(generator = "JDBC")
private Long id;
@NotNull(message = "用户名不能为空",groups = {User.class})
@Size(min =6,message = "用户名最小长度不能小于六位",groups = {User.class})
private String username;
@Email(message = "员工的邮箱不正确",groups = {EmailRegister.class})
private String email;
@Pattern(regexp= RegexPool.MOBILE,message = "手机号格式不正确",groups = {PhoneRegister.class})
private String phone;

private String salt;
@NotNull(message = "用户密码不能为空",groups = {PhoneRegister.class,EmailRegister.class})
@Size(min =6,message = "用户密码最小长度不能小于六位",groups = {PhoneRegister.class})

private String password;

@Transient
private String configPassword;

private Integer state;

private Integer age;

private Date createTime;

private String headImg;

private Boolean del;
@Transient
private String code;

    /**
     * 做一些初始化参数的动作,比如初始化密码,盐,时间等
     * @return
     */
    public User init(){
        //给创建时间赋值
        this.createTime = new Date ();
        //初始化盐值
        this.createSalt();
        //将密码进行md5加密
        this.passwordMD5();
        return this;
    }



    public static void main (String[] args) {

        String s1 = MD5.create ().setSalt ("1".getBytes ()).digestHex ("123456");
        String s = RandomUtil.randomNumbers (8);
        System.out.println ("s = " + s1);
    }
}
